{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pickle \n",
    "import matplotlib.pyplot\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "training_data_file = open(\"mnist_train.csv\", 'r')\n",
    "training_data_list = training_data_file.readlines()\n",
    "training_data_file.close()\n",
    "\n",
    "test_data_file = open(\"mnist_test.csv\", 'r')\n",
    "test_data_list = test_data_file.readlines()\n",
    "test_data_file.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "60000\n",
      "10000\n"
     ]
    }
   ],
   "source": [
    "print(len(training_data_list))\n",
    "print(len(test_data_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_data=np.zeros((60000,784),dtype=np.float32)\n",
    "test_data=np.zeros((10000,784),dtype=np.float32)\n",
    "train_label=np.zeros((60000,10),dtype=np.float32)\n",
    "test_label=np.zeros(10000,dtype=np.float32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "i=0\n",
    "for record in training_data_list:\n",
    "    all_values = record.split(',')\n",
    "    a=[int(i) for i in all_values[1:]]\n",
    "    b=np.array(a)\n",
    "    inputs = (b / 255.0 * 0.99) + 0.01\n",
    "    targets = np.zeros(10) + 0.01\n",
    "    targets[int(all_values[0])] = 0.99\n",
    "    train_data[i]=inputs\n",
    "    train_label[i]=targets\n",
    "    i=i+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "i=0\n",
    "for record in test_data_list:\n",
    "    all_values = record.split(',')\n",
    "    correct_label = int(all_values[0])\n",
    "    inputs = (np.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01\n",
    "    test_data[i]=inputs\n",
    "    test_label[i]=correct_label\n",
    "    i=i+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "f=open('train_data.fms','wb')  \n",
    "pickle.dump(train_data,f)  \n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "f=open('test_data.fms','wb')  \n",
    "pickle.dump(test_data,f)  \n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "f=open('train_label.fms','wb')  \n",
    "pickle.dump(train_label,f)  \n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "f=open('test_label.fms','wb')  \n",
    "pickle.dump(test_label,f)  \n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "f=open('train_data.fms','rb')\n",
    "train_data=pickle.load(f)    \n",
    "f.close()\n",
    "\n",
    "f=open('test_data.fms','rb')\n",
    "test_data=pickle.load(f)    \n",
    "f.close()\n",
    "\n",
    "f=open('train_label.fms','rb')\n",
    "train_label=pickle.load(f)    \n",
    "f.close()\n",
    "\n",
    "f=open('test_label.fms','rb')\n",
    "test_label=pickle.load(f)    \n",
    "f.close()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
